package com.ddwl.wallet.service;

import com.ddwl.common.exception.ParamException;
import com.ddwl.common.feign.api.UserApi;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.schema.bo.wallet.ShopChannelAddBo;
import com.ddwl.schema.bo.wallet.WithdrawChannelAddBo;
import com.ddwl.schema.vo.user.IdentityCardVo;
import com.ddwl.schema.vo.wallet.WithdrawChannelVo;
import com.ddwl.wallet.dao.WithdrawChannelConfDao;
import com.ddwl.wallet.dao.WithdrawChannelDao;
import com.ddwl.wallet.dao.model.UserWithdrawChannel;
import com.ddwl.wallet.dao.model.WithdrawChannelConf;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Admin
 */
@Service
public class WithdrawChannelService {

    @Resource
    private WithdrawChannelDao withdrawChannelDao;
    @Resource
    private WithdrawChannelConfDao withdrawChannelConfDao;
    @Resource
    private UserApi userApi;


    /**
     * 查询用户的提现渠道信息
     * @param userId
     * @return
     */
    public List<WithdrawChannelVo>  queryUserChannel(String userId){
        List<WithdrawChannelVo> list = new ArrayList<>();
        List<WithdrawChannelConf> confList=withdrawChannelConfDao.findEnableConf();
        List<Integer> codes = confList.stream().map(WithdrawChannelConf::getType).collect(Collectors.toList());
        List<UserWithdrawChannel> channelList = withdrawChannelDao.queryUserChannel(userId, codes);
        confList.forEach(conf -> {
            WithdrawChannelVo vo = new WithdrawChannelVo();
            UserWithdrawChannel userWithdrawChannel = channelList.stream().filter(uwChannel -> conf.getType().equals(uwChannel.getType())).findFirst().orElse(null);
            if (null != userWithdrawChannel) {
                vo = BeanConverUtils.convertBean(userWithdrawChannel, WithdrawChannelVo.class);
            }
            vo.setType(conf.getType());
            vo.setName(conf.getName());
            list.add(vo);
        });
        return list;
    }

    /**
     * 查询店铺绑定的银行卡信息
     * @param shopId
     * @return
     */
    public WithdrawChannelVo  queryShopChannel(String shopId){
        UserWithdrawChannel channel = new UserWithdrawChannel();
        channel.setShopId(shopId);
        return BeanConverUtils.convertBean(withdrawChannelDao.selectOne(channel),WithdrawChannelVo.class);
    }

    /**
     * 删除用户的提现渠道信息
     * @param id
     */
    public void deleteUserChannel(String id,String userId){
        UserWithdrawChannel channel = withdrawChannelDao.selectByPrimaryKey(id);
        if(channel!=null&&channel.getUserId().equals(userId)){
            withdrawChannelDao.deleteById(id);
        }
    }

    /**
     * 设置提现渠道
     * @param bo
     */
    public void  withdrawChannelAdd(WithdrawChannelAddBo bo){
        UserWithdrawChannel withdrawChannel = withdrawChannelDao.queryByUserIdAndAccount(bo.getUserId(),bo.getAccount());
        UserWithdrawChannel channel = BeanConverUtils.convertBean(bo,UserWithdrawChannel.class);
        if(withdrawChannel!=null){
            channel.setId(withdrawChannel.getId());
            withdrawChannelDao.updateByPrimaryKeySelective(channel);
        }else{
            withdrawChannelDao.insert(channel);
        }
    }


    public WithdrawChannelVo getUserBankCard(String bankNumber) {
        UserWithdrawChannel withdrawChannel=new UserWithdrawChannel();
        withdrawChannel.setAccount(bankNumber);
        WithdrawChannelVo vo = BeanConverUtils.convertBean(withdrawChannelDao.selectOne(withdrawChannel), WithdrawChannelVo.class);
        return vo;
    }



    public void addShopBankCard(ShopChannelAddBo bo){
        //生成银行卡信息
        //查询用户银行卡信息
        WithdrawChannelVo withdrawChannelVo = getUserBankCard(bo.getAccount());
        if (withdrawChannelVo!=null){
            ParamException.isFalse(withdrawChannelVo.getUserId().equals(bo.getUserId()),"银行卡已经被绑定到其他用户,请联系管理员解绑");
        }
        IdentityCardVo vo = userApi.getIdCardByUserId(bo.getUserId()).getResult();
        bo.setIdcard(vo.getIdCard());
        withdrawChannelAdd(BeanConverUtils.convertBean(bo,WithdrawChannelAddBo.class));
    }

}
